package com.liang.lesson2;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUntils {

    private static  String driver = null;
    private static  String url = null;
    private static  String username = null;
    private static  String password = null;
    static {
        try{
            // 加载 db.properties 配置文件
            InputStream resourceAsStream = JdbcUntils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);  // 修改为正确的加载方法
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
            // 1.驱动只用加载一次
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // 获取连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }
   //释放连接资源
    public static void release(Connection conn, Statement st, ResultSet rs){
            if (rs != null){
                try{
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(st != null){
                try{
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (conn != null){
                try{
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
    }
}



//package com.liang.lesson2;
//
//import java.io.InputStream;
//import java.sql.*;
//        import java.util.Properties;
//
//public class JdbcUntils {
//
//    private static String driver = null;
//    private static String url = null;
//    private static String username = null;
//    private static String password = null;
//
//    static {
//        try {
//            // 加载 db.properties 配置文件
//            InputStream resourceAsStream = JdbcUntils.class.getClassLoader().getResourceAsStream("db.properties");
//            Properties properties = new Properties();
//            properties.load(resourceAsStream);  // 修改为正确的加载方法
//            driver = properties.getProperty("driver");
//            url = properties.getProperty("url");
//            username = properties.getProperty("username");
//            password = properties.getProperty("password");
//
//            // 加载数据库驱动（MySQL 8.x 使用 "com.mysql.cj.jdbc.Driver"）
//            Class.forName("com.mysql.cj.jdbc.Driver");
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    // 获取数据库连接
//    public static Connection getConnection() throws SQLException {
//        return DriverManager.getConnection(url, username, password);
//    }
//
//    // 释放数据库资源
//    public static void release(Connection conn, Statement st, ResultSet rs) {
//        if (rs != null) {
//            try {
//                rs.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        if (st != null) {
//            try {
//                st.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        if (conn != null) {
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                throw new RuntimeException(e);
//            }
//        }
//    }
//}